<?

class PAGE extends OBJECT {

#	If no ID is given, the current page is set up.	
	function __construct( $id = 0, $create_new = false, $type = "page" ) {
		
		$this->type = $type;
		
#		Load assigned object or most recent
		if( $id || !$create_new )
			$this->load( $type, $id );

#		Create new
		else
			$this->create( $type, is_string( $create_new )? $create_new : "..." );
	
#		Set up implemented Classes
		$this->mods = new MOD_CONSTRUCT( $this->values[ 'mods' ] );
	}
	

	public function load( $type, $id ) {
		global $DB;
		
#		Check default vs id vs name
		if( !$id ) $id = $this->track_current();
		if( is_string( $id )) $id = (int) $_SESSION['map']['str'][ $id ]['ID'];

#		Get DB row & decode json strings	
		$this->values = $_SESSION['map']['num'][ (string) $id ];
		
		$this->id = $id;
		$this->values [ 'mods' ] = json_decode( $this->values[ 'mods' ]);
		$this->values [ 'meta' ] = json_decode( $this->values[ 'meta' ]);
		
		if( !$this->name ) $this->name = $this->values [ 'name' ];
		
		return $this->values;
	}
	
	function create( $type, $name = false ) {
		global $DB, $Engine;
		
		$id = (int) $DB->get_key( $type, "ID>0", "max(ID)" ) + 1;
		$DB->query( "insert into pages( type, name, nameid, hierarchy, active, flog, llog, creator ) values ( ". $DB->type( $type ) .", 'p{$id}', ". (( $name )? $Engine->text->set( $name ): 0 ).", 9, 2, now(), now(), '". $Engine->user_get( 'ID' ). "')" );
		
		$_SESSION['map']['num'][ (string) $id ] = $DB->get( "page", (int) $id );
		
		return $this->load( $type, $id );
	}

	
	function track_current() {
		global $DB;
		
		if( !$this->name = $DB->simplestring( $_GET['page'] )) {
		
			$default = current( $_SESSION['map']['menu'] );
			$this->name = $default['name'];
		}
		
		$id = $_SESSION['map']['str'][ $this->name ]['ID'];
		if( !$_SESSION['map']['num'][ $id ]['mods'] )
			$_SESSION['map']['num'][ $id ]['mods'] = $DB->get_key( "page", (int) $id, "mods" );
			
		define( "PAGE", $this->name, true );
		return (int) $id;
	}
	
	
	function visible() {
		global $Engine;
		
		if( !$this->get( 'private' ) || $this->get( 'private' ) <= $Engine->testuser->get( 'rank' ))
			if( $this->get( 'active' ) == 1 ) //|| ( $this->get( 'active' ) && $this->get( 'name' ) == PAGE )
				return true;

	}	
	
	function title() {
		return engine_get('title')." ".engine_get('title.separator')." ".$this->get('title');
	}
	
	
	/**
	 *	define meta content by textid
	 */
	function meta() {
		global $DB, $Engine;
	
		$pid	= $this->track_current();
		$textid	= $DB->get_key( 'page', 'ID = ' . (int) $pid, 'textid' );
		$text	= $Engine->text->get( (int) $textid, 'long' );
		
		return $text;
	}
	
	function hasMeta() {
	
		return ( $this->get( "textid" ));
	}

	function hasParent() {
	
		return ( $this->get( "parent" ));
	}
	
	function meta_uri() {
	
		if( $this->get( "todesc" ) && $subs = $this->get( "sub" ))
			$Meta =  new PAGE(( int ) array_shift( explode( ",", $subs )));

		
		return $Meta? $Meta->uri() : $this->uri();
	}

	
/** 	SET FUNCS
* Functions setting values in page cache and/or DB
* cache happens in the num map					 */

	function set( $key, $value, $is_json = false ) {
		global $DB;
		
		$this->values[ $key ] = $value;
		$_SESSION['map']['num'][ (string) $this->id ][ $key ] = $is_json ? json_encode( $value ): $value;
		
		if( $key != "title" )
			$DB->query( "update pages set `$key`='" . (( $is_json )? json_encode( $value ): $value ) . "' where ID=" . $this->id, 0 );
		
	}

	

/** 	PARSE FUNCS
* Functions returning requested calculations
* Caution, html belong in render_* or "templates" */

	function parse_uri() {
		
		if( $id = (int) $this->get( "redirect" )) {
			$Red = new PAGE( $id );
			$uri = $Red->parse_uri();
		
		} else if( !$uri = $this->get( "uri" ) ) {
			
			$path[] = $this->get("name");
			$parent = $this->get("parent");
			
			while ( $parent ) {
				$path[] = $_SESSION['map']['num'][ $parent ]['name'];
				$parent = $_SESSION['map']['num'][ $parent ]['parent'];
			}
			
			$uri = $_SESSION['map']['num'][ $this->get('ID') ]['uri'] = "/". implode( "/", array_reverse( $path ));
		}
		
		return $uri;
		
		
		/*if( !$uri = $this->get( "uri" ) ) {
			
			// CHECK FOR META CONDITIONS
			//if( $ref = $this->values['meta']->alias ) $this->cycle( $ref );
			
			if( $this->values['meta']->todesc ) {
				$subs = explode( ",", $this->get("sub") );
				//$this->cycle( $subs[ 0 ] );
				$this->load( "page", (int)$subs[ 0 ]);
			}
			
			$path[] = $this->get("name");
			$parent = $this->get("parent");

			while ( $parent ) {
				$path[] = $_SESSION['map']['num'][ $parent ]['name'];
				$parent = $_SESSION['map']['num'][ $parent ]['parent'];
			}
		}
		
		return ( $uri )? $uri : $this->set_cache( $this->get("ID"), "uri", "/". implode( "/", array_reverse( $path )) );*/
	}
	
	function url( $get = "" ) {

		return engine_get( "url" ) .  $this->parse_uri();
	}
	
	function uri() {
		
		return $this->parse_uri();
	}

	
	
	
	
/** 	RENDER FUNCS
* Functions returning html formatted strings
* Caution, big chunks of html belong in "templates" */

	function render_children( $element = "a", $graphic = false ) {
		
		$children = array();
		
		if( $ids = explode( ",", $this->get('sub')) )
		
			foreach( $ids as $id ) {

				$child = new PAGE( (int) $id );
				$active = ($child->name == PAGE);
				if( $child->get("active") == 1 || $active ) {
					$title = ( $graphic )? "<img src='/text.img?tpe=menu.sub&amp;str=".  urlencode( html_entity_decode( $child->get("title"))) ."' alt='". $child->get("title") ."' />" : $child->get("title");
					$node = "<a href='". $child->parse_uri() ."'". ( $active? " class='active'":"" ) .">$title</a>";
					$children[] = ( $element == "a" )? $node : "<$element". ( $active? " class='active'":"" ) .">$node</$element>";
				}
			}
			
		return $children;
	
	}
	
	function render_children_obj() {
		
		$children = array();
		
		if( $ids = explode( ",", $this->get('sub')) )
		
			foreach( $ids as $id ) {

				$child = new PAGE( (int) $id );
				$active = ($child->name == PAGE);
				if( $child->get("active") == 1 || $active ) {
					$children[] = $child;
				}
			}
			
		return $children;
	
	}
	
	function render( $element = "a", $graphic = false ) {

		$active = ($this->name == PAGE);
		if( $this->get("active") == 1 || $active ) {
			$title = ( $graphic )? "<img src='/text.img?tpe=menu.sub&amp;str=".  urlencode( html_entity_decode( $this->get("title"))) ."' alt='". $this->get("title") ."' />" : $this->get("title");
			$node = "<a href='". $this->parse_uri() ."'". ( $active? " class='active'":"" ) .">$title</a>";
			return ( $element == "a" )? $node : "<$element". ( $active? " class='active'":"" ) .">$node</$element>";
		}
	}

	
	function render_a( $graphic = false, $style = "menu.sub" ) {
		
		if( $this->get("active") == 1 || $active = ( $this->name == PAGE )) {
			
			$title = (( $tle = $this->get("title")) && $graphic )? "<img src='/text.img?tpe=$style&amp;str=".  urlencode( html_entity_decode( $tle )) ."' alt='$tle' />" : $tle;
			$node = "<a href='{$this->parse_uri()}'". ( $active? " class='active'":"" ) .">$title</a>";
		}			
		return ( $node )? $node : null;
	}
	
	
	function render_li( $graphic = false, $style = "menu.sub" ) {
		
		return ( $this->get("active") == 1 || $active = ( $this->name == PAGE ))?
			"<li". ( $active? " class='active'":"" ) .">{$this->render_a( $graphic, $style )}</li>" : null;
	}

	
	public function render_core_json ( $return_string = 1 ) {
		
		$object = array( "id"=>$this->id, "type"=> $this->type, "name"=> $this->get( 'name' ), "list"=> array( $this->get( 'title' ), "subs" ));
				
		return ( $return_string )? json_encode( $object ) : $object;
	}


}





?>